package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.fragment.frag_main_dingdanguanli;
import longcaisuyun.longcai.com.net.PostQiang;
import longcaisuyun.longcai.com.suyunbean.QiangItem;


public class QiangResultAcitvity extends Activity implements View.OnClickListener {

    PullToRefreshListView pulllist;
    LinearLayout fanhui_lay;
    List<QiangItem> list = new ArrayList<QiangItem>();
    QiangResultAdapter q = new QiangResultAdapter(QiangResultAcitvity.this, list);
    private frag_main_dingdanguanli dingdanguanli;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qiang_result_acitvity);
        View view = getWindow().getDecorView();
        //调用方法刷新
        dingdanguanli=new frag_main_dingdanguanli();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        ViewInit();
        HttpPost();
    }

    private void ViewInit() {
        fanhui_lay = (LinearLayout) findViewById(R.id.fanhui_lay);
        fanhui_lay.setOnClickListener(this);
        pulllist = (PullToRefreshListView) findViewById(R.id.pulllist);
        pulllist.setScrollLoadEnabled(false);
        pulllist.setPullLoadEnabled(false);
        pulllist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                HttpPost();
            }

            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
            }
        });
        ListView listView = pulllist.getRefreshableView();
        listView.setDivider(null);
        listView.setAdapter(q);
        q.notifyDataSetChanged();
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui_lay:
                dingdanguanli.shuaXin();
                QiangResultAcitvity.this.finish();
                break;
        }
    }

    public void HttpPost() {
        MyApplication.myviewutil.startProgressDialog(QiangResultAcitvity.this);
        new PostQiang(MyApplication.myPreferences.readUid(), new AsyCallBack<PostQiang.Info>() {
            @Override
            public void onSuccess(String toast, int type, PostQiang.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                if (info.message.equals("0")) {
                    Toast.makeText(QiangResultAcitvity.this, "获取数据失败，请稍后重新尝试", Toast.LENGTH_SHORT).show();
                    pulllist.onPullDownRefreshComplete();
                    MyApplication.myviewutil.stopProgressDialog();
                } else {
                    addlist(info.jsonArray);
                    q.notifyDataSetChanged();
                    MyApplication.myviewutil.stopProgressDialog();
                    pulllist.onPullDownRefreshComplete();
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                Toast.makeText(QiangResultAcitvity.this, "获取数据失败，请检查网络设置后重新尝试", Toast.LENGTH_SHORT).show();
                MyApplication.myviewutil.stopProgressDialog();
                pulllist.onPullDownRefreshComplete();
            }
        }).execute(QiangResultAcitvity.this);
    }

    public void addlist(JSONArray jsonArray) {
        list.clear();
        if (jsonArray.length() > 0) {
            for (int j = 0; j < jsonArray.length(); j++) {
                QiangItem qiangItem = new QiangItem();
                try {
                    qiangItem.setTime(jsonArray.getJSONObject(j).getString("time"));
                    qiangItem.setWeek(jsonArray.getJSONObject(j).getString("week"));
                    qiangItem.setOrderid(jsonArray.getJSONObject(j).getString("orderid"));
                    qiangItem.setUsetime(jsonArray.getJSONObject(j).getString("usetime") + jsonArray.getJSONObject(j).getString("week"));
                    qiangItem.setAccount(jsonArray.getJSONObject(j).getString("account"));
//                    qiangItem.setReward(jsonArray.getJSONObject(j).getString("reward"));
                    qiangItem.setCheckinfo(jsonArray.getJSONObject(j).getString("checkinfo"));
                    jsonArray.getJSONObject(j).getJSONArray("addresslist").length();
                    String[] qiangAddresses = new String[jsonArray.getJSONObject(j).getJSONArray("addresslist").length()];
                    for (int i = 0; i < jsonArray.getJSONObject(j).getJSONArray("addresslist").length(); i++) {
                        qiangAddresses[i] = jsonArray.getJSONObject(j).getJSONArray("addresslist").getJSONObject(i).getString("address");
                    }
                    qiangItem.setAddresslist(qiangAddresses);
                    list.add(qiangItem);
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(QiangResultAcitvity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                }
            }
        } else {
            Toast.makeText(QiangResultAcitvity.this, "暂无数据", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dingdanguanli.shuaXin();
    }
}
